package com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starfeedfragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.StarFeedAdapter;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.bean.staritembean.FeedListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 * 动态
 */
public class FeedFragment extends BaseFragment implements FeedContract.View {
    @InjectView(R.id.rv_feed)
    RecyclerView mRecyclerView;

    private List<FeedListBean> listBeen = new ArrayList<>();
    private FeedPresenter presenter;
    private String sid;
    private int page = 1;
    private int lastVisibleItem;
    private LinearLayoutManager linearLayoutManager;
    private StarFeedAdapter adapter;
    private List<FeedListBean> totalList=new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //分页加载
            totalList.addAll(listBeen);
            adapter.setList(totalList);
            if (page==1) {
                mRecyclerView.setAdapter(adapter);
            }else {
                adapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.fragment_starfeed;
    }

    @Override
    public void onLoadStarFeedSuccess(List<FeedListBean> list) {
        listBeen = list;
    }

    @Override
    public void onLoadStarFeedFail(String msg) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initView();
        return rootView;
    }

    /**
     * 初始化视图
     */
    private void initView() {
        adapter=new StarFeedAdapter(getActivity());
        sid = getArguments().getString("sid");
        //mRecyclerView管理器
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        presenter = new FeedPresenter(this, handler);
        presenter.getStarFeedFromNet(sid, page, getActivity());
        //下拉刷新

        //上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) ==adapter
                        .getItemCount()){
                    page++;

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            presenter.getStarFeedFromNet(sid, page, getActivity());
                            adapter.notifyDataSetChanged();

                        }
                    } , 2000);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
